package com.eclipse.e20200201_多线程同步.com.qf.app1.m3;

public class Test {

	public static void main(String[] args) {
		Gene gene = new Gene();

		TaskA taska = new TaskA(gene);
		taska.start();

		TaskB taskb = new TaskB(gene);
		taskb.start();

	}

}

class TaskA extends Thread {
	private Gene gene;

	public TaskA(Gene gene) {
		this.gene = gene;
	}

	public void run() {
		while (true) {
			gene.incre();
			int num = gene.getNum();
			if (num % 2 != 0) {
				System.out.println(num);
			}
		}
	}
}

class TaskB extends Thread {
	private Gene gene;

	public TaskB(Gene gene) {
		this.gene = gene;
	}

	public void run() {
		while (true) {
			gene.incre();
			int num = gene.getNum();
			if (num % 2 != 0) {
				System.out.println(num);
			}
		}
	}
}

/**
 * 偶数生成器
 * 
 * @author Administrator
 *
 */
class Gene {
	private static int num = 0;

	// private Object lock = new Object();

	public void incre() {
		System.out.println("-------------------");
		synchronized (this) {
			num++;
			num++;
			num++;
			num++;
			num++;
			num++;
		}
	}

	public synchronized int getNum() {
		return num;
	}
}
